/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.darly.dlcommon.common.bolts.links;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

/**
 * Provides a set of utility methods for working with incoming Intents that may contain App Link
 * data.
 */
public final class AppLinks {

    static final String KEY_NAME_APPLINK_DATA = "al_applink_data";
    static final String KEY_NAME_EXTRAS = "extras";
    static final String KEY_NAME_TARGET = "target_url";

    /**
     * Gets the App Link data for an intent, if there is any.
     * This is the authorized function to check if an intent is AppLink. If null is returned it is not.
     *
     * @param intent the incoming intent.
     * @return a bundle containing the App Link data for the intent, or {@code null} if none
     * is specified.
     */
    public static Bundle getAppLinkData(Intent intent) {
        return intent.getBundleExtra(KEY_NAME_APPLINK_DATA);
    }

    /**
     * Gets the App Link extras for an intent, if there is any.
     *
     * @param intent the incoming intent.
     * @return a bundle containing the App Link extras for the intent, or {@code null} if none is
     * specified.
     */
    public static Bundle getAppLinkExtras(Intent intent) {
        Bundle appLinkData = getAppLinkData(intent);
        if (appLinkData == null) {
            return null;
        }
        return appLinkData.getBundle(KEY_NAME_EXTRAS);
    }

    /**
     * Gets the target URL for an intent, regardless of whether the intent is from an App Link. If the
     * intent is from an App Link, this will be the App Link target. Otherwise, it will be the data
     * Uri from the intent itself.
     *
     * @param intent the incoming intent.
     * @return the target URL for the intent.
     */
    public static Uri getTargetUrl(Intent intent) {
        Bundle appLinkData = getAppLinkData(intent);
        if (appLinkData != null) {
            String targetString = appLinkData.getString(KEY_NAME_TARGET);
            if (targetString != null) {
                return Uri.parse(targetString);
            }
        }
        return intent.getData();
    }

    /**
     * Gets the target URL for an intent. If the intent is from an App Link, this will be the App Link target.
     * Otherwise, it return null; For app link intent, this function will broadcast APP_LINK_NAVIGATE_IN_EVENT_NAME event.
     *
     * @param context the context this function is called within.
     * @param intent  the incoming intent.
     * @return the target URL for the intent if applink intent; null otherwise.
     */
    public static Uri getTargetUrlFromInboundIntent(Context context, Intent intent) {
        Bundle appLinkData = getAppLinkData(intent);
        if (appLinkData != null) {
            String targetString = appLinkData.getString(KEY_NAME_TARGET);
            if (targetString != null) {
                MeasurementEvent.sendBroadcastEvent(context, MeasurementEvent.APP_LINK_NAVIGATE_IN_EVENT_NAME, intent, null);
                return Uri.parse(targetString);
            }
        }
        return null;
    }
}
